package net.nohaven.proj.javeau.crypt;

import net.nohaven.proj.javeau.crypt.engine.Phelix;
import net.nohaven.proj.javeau.utils.Utils;

public class PwdUtils {
	public static byte[] strengthenPassword(byte[] pwd, int targetLen) {
		byte[] ret = new byte[targetLen];
		byte[] ret2 = new byte[targetLen];

		byte[] key = new byte[32];
		byte[] iv = new byte[16];

		Phelix phelix = new Phelix(Phelix.PHELIX_MAC_SIZE);

		for (int i = 0; i < pwd.length; i++) {
			key[i & 0x1F] ^= pwd[i];
			iv[i & 0xF] ^= pwd[i];
		}

		int alignment = Utils.alignToWord(pwd.length, 4);

		byte[] alignedPwd = new byte[alignment];
		System.arraycopy(pwd, 0, alignedPwd, 0, pwd.length);
		byte[] buf = new byte[alignment];

		int cycles = 20971520 / alignedPwd.length;

		for (int i = 0; i < cycles; i++)
			phelix.process(alignedPwd, 0, buf, 0, alignment);

		phelix.process(ret2, 0, ret, 0, targetLen);
		return ret;
	}
}
